package com.lanzhou.yuanfen.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanzhou.yuanfen.diary.entity.Archive;
import com.lanzhou.yuanfen.diary.service.IArchiveService;
import com.lanzhou.yuanfen.response.ServerResponsePage;
import com.lanzhou.yuanfen.response.ServerResponseResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/archive/")
public class ArchiveController {

    @Resource
    private IArchiveService iArchiveService;


    /**
     * 获取归档
     *
     * @return
     */
    @PostMapping("getAllArchive")
    public ServerResponseResult getAllArchive() {
        List<Archive> archives = iArchiveService.redisCache();
        return ServerResponseResult.success(archives);
    }


    /**
     * 获取父节点归档
     *
     * @return
     */
    @PostMapping("getAllFatherArchive")
    public ServerResponseResult getAllFatherArchive() {
        List<Archive> archives = iArchiveService.list(new QueryWrapper<Archive>().eq("father_node", 0));
        return ServerResponseResult.success(archives);
    }


    /**
     * 添加/修改归档
     *
     * @param archive
     * @return
     */
    @PostMapping("addOrUpdateArchive")
    public ServerResponseResult addOrUpdateArchive(Archive archive) {
        archive.setUpdateTime(LocalDateTime.now());
        boolean save = iArchiveService.saveOrUpdate(archive);
        return save ? ServerResponseResult.success(archive) : ServerResponseResult.fail();
    }


    /**
     * 删除文章
     *
     * @param archiveKey
     * @return
     */
    @PostMapping("delArchive")
    public ServerResponseResult delArchive(@RequestParam("archiveKey") String archiveKey) {
        boolean save = iArchiveService.removeById(archiveKey);
        return save ? ServerResponseResult.success() : ServerResponseResult.fail();
    }


    /**
     * 添加文章
     *
     * @param pageSize
     * @param pageIndex
     * @return
     */
    @PostMapping("pageGetArchive")
    public ServerResponsePage pageGetArchive(@RequestParam("pageSize") Integer pageSize,
                                             @RequestParam("pageIndex") Integer pageIndex) {
        ServerResponsePage<Archive> responsePage = new ServerResponsePage<>();
        IPage<Archive> iPage = new Page<>(pageIndex, pageSize);
        IPage<Archive> page = iArchiveService.page(iPage, new QueryWrapper<Archive>()
                .orderByDesc("archive_key"));
        setParentNode(page.getRecords());
        responsePage.setIPage(page);
        return responsePage;
    }


    private void setParentNode(List<Archive> records) {
        List<Archive> archives = iArchiveService.list();
        if (records.size() == 0) {
            return;
        }
        Map<Long, Archive> archiveMap = archives.stream().collect(Collectors.toMap(Archive::getArchiveKey, x -> x));
        for (Archive record : records) {
            Long fatherNode = record.getFatherNode();
            if (fatherNode > 0) {
                Archive archive = archiveMap.get(fatherNode);
                record.setParentNode(archive);
            }
        }
    }


}
